package vn.mahjonggame.zone.event;

import vn.mahjonggame.metadata.GameActionType;
import vn.mahjonggame.metadata.UserVarName;
import vn.mahjonggame.game.MahjongExtension;
import vn.mahjonggame.platform.PlatformClient;
import vn.mahjonggame.platform.PlatformConfig;
import vn.mahjonggame.util.Common;
import vn.mahjonggame.zone.MahjongZoneExtension;

import com.smartfoxserver.bitswarm.sessions.Session;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSConstants;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class OnUserLoginHandler extends BaseServerEventHandler {

	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		// TODO Auto-generated method stub		
		Session session = (Session) event.getParameter(SFSEventParam.SESSION);
		SFSObject sfsParams = (SFSObject) event.getParameter(SFSEventParam.LOGIN_IN_DATA);		
		
		String user_id;
		String displayname;
		String avatar;
		String gender;
		
		String token = sfsParams.getUtfString(UserVarName.token.toString());			
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": token " + token);
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": start get_access_token");
		SFSObject access_tokenObj = PlatformClient.get_access_token(token);
					
		String access_token = null;
		
		if(Common.isApiSuccess(access_tokenObj)){
			access_token = access_tokenObj.getSFSObject("response").getUtfString(UserVarName.access_token.toString());			
		}
		else {			
			String msgText = "";
			if(access_tokenObj != null){
				access_tokenObj.get("msgText").getObject().toString();
			}
			String msg = GameActionType.login.toString() + ": Failed in get_access_token API " + msgText;
			
			trace(ExtensionLogLevel.ERROR, msg);
			access_token = "debug_access_token";
		}
		
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": end get_access_token");
		
		SFSObject sfsUserInfo = null;
		
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": start user_get_info");
		
		SFSObject userInfoResponse = PlatformClient.user_get_info(access_token);
		
		if(Common.isApiSuccess(userInfoResponse)){
			sfsUserInfo = (SFSObject) userInfoResponse.getSFSObject("response");
		}
		else {
			String msg = GameActionType.login.toString() + ": Failed user_get_info API in authentication";
			trace(ExtensionLogLevel.ERROR, msg);
			throw new SFSLoginException(msg);
		}
		
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": end user_get_info");
		
		user_id = sfsUserInfo.getUtfString(UserVarName.user_id.toString());
		displayname = sfsUserInfo.getUtfString(UserVarName.displayname.toString());
		avatar = sfsUserInfo.getUtfString(UserVarName.avatar.toString());
		if(avatar == null)
			avatar = PlatformConfig.configMap.get(PlatformConfig.DEFAULT_AVATAR);
		gender = sfsUserInfo.getUtfString(UserVarName.gender.toString());
							
		/*
		 * Prepare response data for client
		 */
		session.setProperty(UserVarName.user_id.toString(), user_id);		
		session.setProperty(UserVarName.access_token.toString(), access_token);				
		session.setProperty(UserVarName.displayname.toString(), displayname);
		session.setProperty(UserVarName.avatar.toString(), avatar);
		session.setProperty(UserVarName.gender.toString(), gender);
		
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": user_id " + user_id);
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": access_token " + access_token);
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": avatar " + avatar);
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": displayname " + displayname);
		trace(ExtensionLogLevel.INFO, GameActionType.login.toString() + ": gender " + gender);

		ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
		outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, displayname);			
	}
}
